<?php

class PdfController extends AppController {

	var $name = 'Pdf';
	var $helpers = array('batch');
    var $layout = 'default';
    var $pageTitle = 'PDF Creator';
    var $components = array('Functions');

	function beforeRender() {
		$groupid = $this->Auth->user('group_id');
		$name = 'PDF';
		IF($groupid != 1){
			$result1 = mysql_query("SELECT id FROM applications WHERE name = '$name'");
			$appid = mysql_fetch_object($result1)->id;
			$result2 = mysql_query("SELECT count(*) as count FROM groupshasrights WHERE group_id = '$groupid' AND application_id = '$appid'");
			$count = mysql_fetch_object($result2)->count;
			IF($count == 0){
				$this->Session->setFlash(__('Access Denied', true));
				$this->redirect(array('controller' => 'pages' ,'action' => 'error'));	
			}
		}
	}
    
    
    function index() {
    	$result = mysql_query("SELECT id, name, description FROM projects");
    	$this->set(compact('result'));
    }
    
    function report(){
    	$pid = $this->params['url']['pid'];
    	$checkdtv = $this->Functions->checkdtv2($pid);
    	$this->set(compact('pid','checkdtv'));
    }
    
    
	function report1(){
        $this->layout = 'pdf'; //this will use the pdf.ctp layout
        $user = $this->Session->read('Auth.User');        
        $name = $user["name"];
        $vorname = $user["vorname"];
        $this->set(compact('name','vorname'));
        $pid = $this->params['url']['pid'];
        $result2 = mysql_query("SELECT name,description FROM projects WHERE id = '$pid'");
        $row = mysql_fetch_array($result2);
        $pname = $row["name"];
        $pdescription = $row["description"];
        $result = mysql_query("SELECT r.id rid, s.order FROM runs AS r, runshasstatusruns AS rhs, statuses AS s, projects AS p, (SELECT max(rhs.id) AS maxid, rhs.run_id AS runid FROM runshasstatusruns AS rhs GROUP BY rhs.run_id) AS x WHERE r.id = rhs.run_id AND s.id = rhs.status_id AND x.runid = r.id AND rhs.id = x.maxid AND statusgroup_id = 1 AND p.id = r.project_id AND p.id = '$pid'");
        $this->set(compact('result','pname','pdescription'));
	}
	
	
	function report2(){
        $this->layout = 'pdf'; //this will use the pdf.ctp layout
        $user = $this->Session->read('Auth.User');        
        $name = $user["name"];
        $vorname = $user["vorname"];
        $this->set(compact('name','vorname'));
        $pid = $this->params['url']['pid'];
        $result2 = mysql_query("SELECT name,description FROM projects WHERE id = '$pid'");
        $row = mysql_fetch_array($result2);
        $pname = $row["name"];
        $pdescription = $row["description"];
        $result = mysql_query("SELECT r.id FROM runs AS r, projects AS p  WHERE  p.id = r.project_id AND p.id = '$pid' ORDER BY r.id ASC");
		$this->set(compact('result','pname','pdescription'));
	}
	

	
	
	function report3(){
		$this->layout = 'pdf'; //this will use the pdf.ctp layout
		$user = $this->Session->read('Auth.User');
		$name = $user["name"];
		$vorname = $user["vorname"];
		$this->set(compact('name','vorname'));
		$pid = $this->params['url']['pid'];
		$result2 = mysql_query("SELECT name,description FROM projects WHERE id = '$pid'");
        $row = mysql_fetch_array($result2);
        $pname = $row["name"];
        $pdescription = $row["description"];
		$result = mysql_query("SELECT mst.id, mst.name, mst.reportname FROM projects p, projectshasmacservicetypes phs, macservicetypes mst WHERE p.id = phs.project_id AND phs.macservicetype_id = mst.id AND p.id = '$pid'");
		$this->set(compact('pname','pid','result','pdescription'));
	}

	
	function report4(){
        $this->layout = 'pdf'; //this will use the pdf.ctp layout
        $user = $this->Session->read('Auth.User');        
        $name = $user["name"];
        $vorname = $user["vorname"];
        $this->set(compact('name','vorname'));
        $pid = $this->params['url']['pid'];
        $result2 = mysql_query("SELECT name,description FROM projects WHERE id = '$pid'");
        $row = mysql_fetch_array($result2);
        $pname = $row["name"];
        $pdescription = $row["description"];
        $result = mysql_query("SELECT r.id FROM runs AS r, projects AS p  WHERE  p.id = r.project_id AND p.id = '$pid' ORDER BY r.id ASC");
		$this->set(compact('result','pname','pdescription'));
	}
}
?>